package cn.iocoder.yudao.im.controller.admin;


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.im.domain.NoticeImMessage;
import cn.iocoder.yudao.im.service.NoticeImMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 消息表 前端控制器
 * </p>
 *
 * @author zcc
 * @since 2024-05-22
 */
@RestController
@RequestMapping("/message/private")
@RequiredArgsConstructor
@Slf4j
public class NoticeImMessageController {


    @Resource
    NoticeImMessageService fancyOfficeImMessageService;

    @TenantIgnore
    @PostMapping("/send")
    public CommonResult sendMessage(@Valid @RequestBody NoticeImMessage vo) {
        return CommonResult.success(fancyOfficeImMessageService.sendMessage(vo));
    }


    @TenantIgnore
    @DeleteMapping("/recall/{id}")
    public CommonResult recallMessage(@NotNull(message = "消息id不能为空") @PathVariable Long id) {
        CommonResult CommonResult = fancyOfficeImMessageService.recallMessage(id);
        return CommonResult;
    }

    @TenantIgnore
    @GetMapping("/pullOfflineMessage")
    public CommonResult pullOfflineMessage(@RequestParam("minId") Long minId) {
        fancyOfficeImMessageService.pullOfflineMessage(minId);
        return CommonResult.success("");
    }

    @TenantIgnore
    @PutMapping("/readed")
    public CommonResult readedMessage(@RequestParam Long friendId) {
        fancyOfficeImMessageService.readedMessage(friendId);
        return CommonResult.success("");
    }

    @TenantIgnore
    @GetMapping("/maxReadedId")
    public CommonResult getMaxReadedId(@RequestParam("userId") Long userId) {
        return CommonResult.success(fancyOfficeImMessageService.getMaxReadedId(userId));
    }

    @TenantIgnore
    @GetMapping("/history")
    public CommonResult recallMessage(@NotNull(message = "好友id不能为空") @RequestParam Long friendId,
                                      @NotNull(message = "页码不能为空") @RequestParam Long page,
                                      @NotNull(message = "size不能为空") @RequestParam Long size) {
        return CommonResult.success(fancyOfficeImMessageService.findHistoryMessage(friendId, page, size));
    }

    @TenantIgnore
    @PostMapping("/save")
    public CommonResult save(@RequestBody NoticeImMessage noticeImMessage) {
        return CommonResult.success(fancyOfficeImMessageService.save(noticeImMessage));
    }

//    @DeleteMapping
//    public CommonResult delete() {
//        LocalDateTime sevenDaysAgo = LocalDateTime.now().minusDays(30);
//        FancyOfficeImMessage fancyOfficeImMessage = new FancyOfficeImMessage();
//        fancyOfficeImMessage.setStatus(MessageStatus.DELETE.code());
//        boolean update = fancyOfficeImMessageService.update(fancyOfficeImMessage, new LambdaQueryWrapper<FancyOfficeImMessage>().lt(FancyOfficeImMessage::getSendTime, sevenDaysAgo));
//        log.info("<<<<<开始清除30天前的聊天记录>>>>> 是否成功：{} >>>>>每天一点执行一次！", update);
//        return CommonResult.success();
//    }

}
